<template>
  <div class="select">
    <a-select
      v-model:value="selectValue"
      style="width: 100%"
      placeholder="请选择"
      :options="options"
      :fieldNames="fieldNames"
      allowClear
      @change="handleChange"
    ></a-select>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch, nextTick } from "vue";
import { defHttp } from "/@/utils/http/axios";
export default defineComponent({
  name: "rw_select",
  props: {
    code: {
      type: String,
      default: "org",
    },
    value: {
      type: String,
      default: "",
    },
    label: {
      type: String,
      default: "typeName",
    },
    codevalue: {
      type: String,
      default: "typeCode",
    },
  },
  setup(props, context) {
    const selectValue = ref<string | number>();
    const { code, label, codevalue } = props;
    const options = ref([]);
    const fieldNames = {
      label,
      value: codevalue,
    };
    watch(
      () => props.value,
      (n) => {
        if (n) {
          selectValue.value = n + "";
        } else {
          selectValue.value = "";
        }
      }
    );
    const handleChange = (value: string) => {
      context.emit("input", value);
      context.emit("change", value);
    };

    // 根据type不同调用不同的接口
    const getselectList = () => {
			let url = `key-values/Select/${code}`;
			
      defHttp.get({ url }).then((res) => {
        options.value = res.data;
      });
    };
    getselectList();
    return {
      selectValue,
      fieldNames,
      options,
      handleChange,
    };
  },
});
</script>
